package cn.ljlao.mall.product.goods.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.ljlao.common.utils.PageUtils;
import cn.ljlao.common.utils.R;
import cn.ljlao.mall.product.goods.entity.AttrAttrgroupRelationEntity;
import cn.ljlao.mall.product.goods.entity.AttrEntity;
import cn.ljlao.mall.product.goods.service.AttrAttrgroupRelationService;
import cn.ljlao.mall.product.goods.service.AttrService;
import cn.ljlao.mall.product.goods.service.CategoryService;
import cn.ljlao.mall.product.goods.vo.AttrAttrgroupRelationVo;
import cn.ljlao.mall.product.goods.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.ljlao.mall.product.goods.entity.AttrGroupEntity;
import cn.ljlao.mall.product.goods.service.AttrGroupService;


/**
 * 属性分组
 *
 * @author LiuZhen
 * @email ljlao@vip.qq.com
 * @date 2020-10-14 13:51:59
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    ///product/attrgroup/1/attr/relation?t=1618386920264
    ///product/attrgroup/{attrgroupId}/attr/relation
    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    /**
     * /product/attrgroup/{catelogId}/withattr
     * 获取分类下所有分组&关联属性
     * @param catelogId
     * @return
     */

    @GetMapping("/{catelogId}/withattr")
    public R attrgroupCatelogIdWithattrs(@PathVariable("catelogId") Long catelogId){

        List<AttrGroupWithAttrsVo> vos = attrGroupService.attrgroupCatelogIdWithattrs(catelogId);
        return R.ok().put("data",vos);
    }


    ///product/attrgroup/attr/relation/delete
    @RequestMapping("/attr/relation/delete")
    public R deleteAttrRelation(@RequestBody List<AttrAttrgroupRelationEntity> list) {
        attrService.deleteAttrRelation(list);

        return R.ok();
    }

    /**
     * 查询分组关联属性
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    //@RequiresPermissions("goods:attrgroup:list")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        // PageUtils page = attrGroupService.queryPage(params,catId);
        List<AttrEntity> list = attrService.attrRelation(attrgroupId);
        return R.ok().put("data", list);
    }

    /**
     * 查询 关联关系
     * @param attrgroupId
     * @param params
     * @return
     */
    ///product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R noAttrRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params) {

        PageUtils page = attrService.noAttrRelation(attrgroupId, params);
        return R.ok().put("page", page);
    }
    ///product/attrgroup/attr/relation

    @PostMapping("/attr/relation")
    public R attrRelation(@RequestBody List<AttrAttrgroupRelationVo> list){
        attrAttrgroupRelationService.saveBatch(list);

        return R.ok();
    }

    /**
     * 列表
     */
    @GetMapping("/list/{catId}")
    //@RequiresPermissions("goods:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catId") Long catId) {
        PageUtils page = attrGroupService.queryPage(params, catId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("goods:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        //所属分类id
        Long catelogId = attrGroup.getCatelogId();
        attrGroup.setCatelogIds(categoryService.getCatePath(catelogId));
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("goods:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("goods:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("goods:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
